package com.example.zhihuijiaju;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import static android.content.Context.MODE_PRIVATE;

public class FileOperations {
    private static final String TAG = "FileOperations";
    static void file_creat(Context context,String file_name) {
        File path = context.getFilesDir();
        Log.i(TAG, path.toString());
        File file = new File(path, file_name);
        try {
            file.createNewFile();
            Log.i(TAG, "创建文件成功");
        } catch (
                IOException e) {
            e.printStackTrace();
            Log.i(TAG, "创建文件失败");
        }
    }

    static void file_write(Context context,String file_name,String data) {
        try (
                FileOutputStream outputStream = context.openFileOutput(file_name, MODE_PRIVATE)) {
            outputStream.write(data.getBytes());
            outputStream.close();
            Log.i(TAG, "写入成功");

        } catch (
                FileNotFoundException e) {
            Log.i(TAG, "写入失败");
            e.printStackTrace();
        } catch (IOException e) {
            Log.i(TAG, "写入失败");
            e.printStackTrace();
        }
    }
    static String file_read(Context context, String file_name){
        try {
            FileInputStream fileOutputStream = context.openFileInput(file_name);
            byte []butter = new byte[fileOutputStream.available()];
            fileOutputStream.read(butter);
            fileOutputStream.close();
            String content = new String(butter);
            Log.i(TAG,"读取成功");
          //  Log.i(TAG, content);
            return content;
        } catch (FileNotFoundException e) {
            Log.i(TAG,"读取失败");
            e.printStackTrace();
        } catch (IOException e) {
            Log.i(TAG,"读取失败");
            e.printStackTrace();
        }
        return "";
    }


}
